package meta

import (
	"gitee.com/micro-plat/codeless/apiserver/meta/biz/meta"
	"github.com/micro-plat/hydra"
)

// AddColumnHandle 添加字段元数据的处理函数
func AddColumnHandle(ctx hydra.IContext) interface{} {
	if err := ctx.Request().Check("title", "name", "type", "source", "table_id"); err != nil {
		return err
	}

	columnTitle := ctx.Request().GetString("title")
	columnName := ctx.Request().GetString("name")
	columnType := ctx.Request().GetString("type")
	source := ctx.Request().GetString("source")
	method := ctx.Request().GetString("method")
	param := ctx.Request().GetString("param")
	isNullable := ctx.Request().GetBool("is_nullable")
	isDictLabel := ctx.Request().GetBool("is_dict_label")
	isLikeQuery := ctx.Request().GetBool("is_like_query")
	isPrefab := ctx.Request().GetBool("is_prefab")
	description := ctx.Request().GetString("title")
	tenantID := ctx.Request().GetInt("tenant_id")
	tableId := ctx.Request().GetInt64("table_id")

	columnID, err := meta.AddColumn(tenantID, tableId, columnTitle, columnName,
		columnType, isNullable, isDictLabel, isLikeQuery, source, method, param, isPrefab, description)
	if err != nil {
		return err
	}

	return map[string]interface{}{
		"column_id": columnID,
	}
}
func UpdateColumnHandle(ctx hydra.IContext) interface{} {
	if err := ctx.Request().Check("title", "name", "type", "source", "column_id"); err != nil {
		return err
	}
	columnTitle := ctx.Request().GetString("title")
	columnName := ctx.Request().GetString("name")
	columnType := ctx.Request().GetString("type")
	source := ctx.Request().GetString("source")
	method := ctx.Request().GetString("method")
	param := ctx.Request().GetString("param")
	isNullable := ctx.Request().GetBool("is_nullable")
	isDictLabel := ctx.Request().GetBool("is_dict_label")
	isLikeQuery := ctx.Request().GetBool("is_like_query")
	isPrefab := ctx.Request().GetBool("is_prefab")
	description := ctx.Request().GetString("title")
	tenantID := ctx.Request().GetInt("tenant_id")
	columnID := ctx.Request().GetInt64("column_id")
	err := meta.UpdateColumn(tenantID, columnID, columnTitle, columnName,
		columnType, isNullable, isDictLabel, isLikeQuery, source, method, param, isPrefab, description)
	if err != nil {
		return err
	}
	return map[string]interface{}{
		"column_id": columnID,
	}
}
